package com.sh.redistest;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.*;

/**
 * @ClassName TestAPI
 * @Description redis基本API
 * @Author yaoqiang
 * @Date 2020/10/20 13:23
 * @Version 1.0
 */
public class TestAPI {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.55.60", 6379);

        System.out.println(jedis.ping());
        jedis.set("k1", "v1");
        jedis.set("k2", "v2");
        Set<String> keys = jedis.keys("*");
//        for (Iterator iterator = keys.iterator(); iterator.hasNext(); ) {
//            String key = (String) iterator.next();
//            System.out.println(key+":"+jedis.get(key));//k1:v1 k2:v2
//        }
        System.out.println("jedis.exist==="+jedis.exists("k1"));//jedis.exist===true
        System.out.println(jedis.ttl("k1"));//-1
        jedis.append("k1"," myredis");
        System.out.println(jedis.get("k1"));//v1 myredis
        System.out.println("--------------------set--------------------");
        jedis.mset("str1","v1","str2","v2","str3","v3");
        System.out.println(jedis.mget("str1","str2","str3"));//[v1, v2, v3]
        System.out.println("---------------list-------------------------");
        jedis.lpush("mylist","v1","v2","v3");
        List<String> list=jedis.lrange("mylist",0,-1);
        for (String ele:list){
            System.out.println(ele);
        }
        //set
        jedis.sadd("orders","jd001");
        jedis.sadd("orders","jd002");
        Set<String> set1=jedis.smembers("orders");
        for (Iterator iterator=set1.iterator();iterator.hasNext();){
            System.out.println(iterator.next());
        }
        jedis.srem("orders","jd002");//删除
        System.out.println(jedis.smembers("orders").size());//1
        //hash
        jedis.hset("hash1","username", "yaoq");
        System.out.println(jedis.hget("hash1","username"));
        Map<String,String> map =new HashMap<String,String>();
        map.put("pwd","123");map.put("age","28");
        jedis.hmset("hash2",map);
        List<String> result=jedis.hmget("hash2","username","age");
        for (String str:result){
            System.out.println(str);
        }
//zset
        jedis.zadd("zset01",1,"v1");
        jedis.zadd("zset01",2,"02");
        Set<String> s1=jedis.zrange("zset01",0,-1);
        for (Iterator iterator=s1.iterator();iterator.hasNext();){
            System.out.println(iterator.next());
        }

        jedis.watch("k1");
      //  jedis.unwatch();
        Transaction transaction=jedis.multi();
        transaction.exec();

    }
}
